package Java8实战._3_Stream_用流收集数据._1_收集器简介;

import java.util.*;
import java.util.stream.Collectors;

public class _1_用指令式风格对交易按照货币分组 {

    public static void main(String[] args) {

        List<Transaction> transactions = Arrays.asList(
                new Transaction(),
                new Transaction(),
                new Transaction(),
                new Transaction(),
                new Transaction()
        );

        //建立累积交易分组的Map
        //Currency - 货币
        //Transaction - 交易
        Map<Currency, List<Transaction>> transactionsByCurrencies = new HashMap<>();


        //迭代Transaction的List
        for (Transaction transaction : transactions){
            //提取Transaction的货币
            Currency currency = transaction.getCurrency();

            List<Transaction> transactionsForCurrency = transactionsByCurrencies.get(currency);

            //如果分组Map中没有这种货币的条目，就创建一个
            if(transactionsForCurrency == null){
                transactionsForCurrency = new ArrayList<>();
                transactionsByCurrencies.put(currency, transactionsForCurrency);
            }

            transactionsForCurrency.add(transaction);
        }


        //Lambda
        Map<Currency, List<Transaction>> transactionsByCurrencies1 = transactions.stream()
                .collect(Collectors.groupingBy(Transaction::getCurrency));
    }
}






























































